Coding Art by Yu Zhang & Mathias Funk

Coding Art by Yu Zhang & Mathias Funk

Author:Yu Zhang & Mathias Funk
Language: eng
Format: epub
ISBN: 9781484262641
Publisher: Apress


? Think about this

Try extending this example by checking the SHIFT key to make the rectangle move faster in one direction when pressing SHIFT. What else could you influence with the keyboard?

Precise control is a strength of the keyboard; every key press counts. As we will see in the next example, we can also work with the content of the keys that are pressed. Let’s start by simply printing the pressed key as a bold character on the canvas.

This example uses a brief setup and an empty draw function, and most of the action happens in the keyPressed function . In the beginning, we load a font that we previously created with Processing tools. How did we do that? Open Processing and the Tools menu. There is an option “Create font...” that allows to pick a font from your computer, determine the size, and convert it into a format that Processing can use directly. It works better if you specify the right font size already, so the rendered text is sharp in the end. Processing creates a new font file that should be located in the same folder as the Processing sketch.

Print the pressed key as a bold character on the canvas

PFont f;

void setup() {

size(400, 400);

// load a specific font to print text

f = loadFont("InterUI-ExtraBold-250.vlw");

background(0);

}

void draw() {}

void keyPressed() {

// draw character

background(0); fill(255);

// set text rendering options

textFont(f, 250);

textSize(250);

// measure character width

float charWidth = textWidth(key);

// draw character centered

text(key, (width - charWidth) / 2., 300);

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.